﻿Imports System.Globalization
Imports DotNetNuke.Modules.Forum
Imports DotNetNuke.Common.Utilities

Namespace Aricie.DNN.Services.Url.FriendlyUrl.Providers
    Public Class DnnForumUrlRewriter
        Implements IUrlRewriterProvider
        Implements IIsActive


        Public Function GetModuleRewrites() As List(Of GroupParamsRewrite) Implements IUrlRewriterProvider.GetRewrites
            Dim toReturn As New List(Of GroupParamsRewrite)
            Dim desc As String = "Name of the current forums' group browsed, filtered accordingly to the general parameters"
            toReturn.Add(New GroupParamsRewrite("GroupName", desc, RewriteType.SubPath, UrlParam.FromSyntax("groupid")))
            desc = "Name of the current forum browsed, or the current thread's forum, filtered accordingly to the general parameters"
            toReturn.Add(New GroupParamsRewrite("ForumName", desc, RewriteType.SubPath, UrlParam.FromSyntax("forumid")))
            desc = "Name of the current thread browsed, or the current post's thread, filtered accordingly to the general parameters"
            toReturn.Add(New GroupParamsRewrite("ThreadName", desc, RewriteType.SubPath, UrlParam.FromSyntax("threadid")))
            Return toReturn
        End Function

        Public Function RewriteParams(ByVal groupName As String, ByVal objRewriteType As RewriteType, ByVal params As Dictionary(Of UrlParam, String), _
                        ByVal behaviour As FriendlierUrlStrategy) As GroupRewriteResult Implements IUrlRewriterProvider.RewriteParams
            Dim toReturn As New GroupRewriteResult()
            Select Case groupName.ToLowerInvariant
                Case "groupname"
                    Dim groupeId As Integer = Integer.Parse(params(UrlParam.FromSyntax("groupid")), CultureInfo.InvariantCulture)
                    toReturn.RewriteValue = GetGroupName(groupeId, behaviour)
                    toReturn.ConsumedParameters(UrlParam.FromSyntax("groupid")) = True
                Case "forumname"
                    Dim forumId As Integer = Integer.Parse(params(UrlParam.FromSyntax("forumid")), CultureInfo.InvariantCulture)
                    toReturn.RewriteValue = GetForumName(forumId, behaviour)
                    toReturn.ConsumedParameters(UrlParam.FromSyntax("forumid")) = True
                Case "threadname"
                    Dim threadId As Integer = Integer.Parse(params(UrlParam.FromSyntax("threadid")), CultureInfo.InvariantCulture)
                    toReturn.RewriteValue = GetThreadName(threadId, behaviour)
                    toReturn.ConsumedParameters(UrlParam.FromSyntax("threadid")) = True
            End Select
            Return toReturn
        End Function

        Public Function GetGroupName(ByVal groupeId As Integer, ByVal strategy As FriendlierUrlStrategy) As String
            Dim GroupInf As GroupInfo = New GroupController().GroupGet(groupeId)
            Dim toReturn As String = String.Empty
            If GroupInf IsNot Nothing Then
                toReturn = GroupInf.Name
            End If
            'Return strategy.StringEscaper.EscapeString(toReturn)
            Return toReturn
        End Function

        Public Function GetForumName(ByVal forumId As Integer, ByVal strategy As FriendlierUrlStrategy) As String
            Dim fc As New ForumController

            'Dim ForumInf As ForumInfo = fc.GetForum(forumId)
            Dim ForumInf As ForumInfo = DirectCast(CBO.FillObject(Of ForumInfo)(DataProvider.Instance().ForumGet(forumId)), ForumInfo)
            Dim toReturn As String = String.Empty
            If ForumInf IsNot Nothing Then
                toReturn = ForumInf.Name
            Else
                Throw New ApplicationException(String.Format("Forum with forumId {0} not found for url rewriting", forumId.ToString(CultureInfo.InvariantCulture)))
            End If
            'Return strategy.StringEscaper.EscapeString(toReturn)
            Return toReturn
        End Function

        Public Function GetThreadName(ByVal threadId As Integer, ByVal strategy As FriendlierUrlStrategy) As String
            Dim tc As New ThreadController
            'Dim ThreadInf As ThreadInfo = tc.ThreadGet(threadId)
            Dim ThreadInf As ThreadInfo = DirectCast(CBO.FillObject(Of ThreadInfo)(DataProvider.Instance().ThreadGet(threadId)), ThreadInfo)
            Dim toReturn As String = String.Empty
            If ThreadInf IsNot Nothing Then
                toReturn = ThreadInf.Subject
            Else
                Throw New ApplicationException(String.Format("Thread with threadId {0} not found for url rewriting", threadId.ToString(CultureInfo.InvariantCulture)))
            End If
            'Return strategy.StringEscaper.EscapeString(toReturn)
            Return toReturn
        End Function

        Public ReadOnly Property IsActive As Boolean Implements IIsActive.IsActive
            Get
                Return DotNetNuke.Framework.Reflection.CreateType("DotNetNuke.Modules.Forum.ThreadController, DotNetNuke.Modules.Forum", True) IsNot Nothing
            End Get
        End Property
    End Class
End Namespace